﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace An_Im
{
    internal static class ExtendStringHelper
    {


        /// <summary>
        /// 将字符串转换为整形。
        /// </summary>
        public static int ToInt(this string str, int defaultValue = 0)
        {
            var valid = int.TryParse(str, out var result);
            if (valid)
                return result;
            else
                return defaultValue;
        }

        /// <summary>
        /// 将字符串转换为双精度浮点型。
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static double ToDouble(this string str, double defaultValue = 0d)
        {
            var valid = double.TryParse(str, out var result);
            if (valid)
                return result;
            else
                return defaultValue;
        }


        /// <summary>
        /// 将字符串转化为单精度浮点型。
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static float ToFloat(this string str, float defaultValue = 0f)
        {
            var valid = float.TryParse(str, out var result);
            if (valid)
                return result;
            else
                return defaultValue;
        }

    }
}
